"""
按钮组件，实现类似Element Plus的按钮样式和功能
"""
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import pyqtProperty, QSize
from component.theme import Theme


class Button(QPushButton):
    """
    按钮组件，支持多种类型、尺寸和状态
    
    属性:
        button_type: 按钮类型，可选值为 primary, success, warning, danger, info
        size: 按钮尺寸，可选值为 large, default, small, mini
        plain: 是否为朴素按钮（仅显示边框）
        round: 是否为圆角按钮
        disabled: 是否禁用
    """
    
    def __init__(self, text="", parent=None, button_type="primary", size="default", 
                 plain=False, round=False):
        """
        初始化按钮
        
        Args:
            text: 按钮文本
            parent: 父组件
            button_type: 按钮类型，可选值为 primary, success, warning, danger, info
            size: 按钮尺寸，可选值为 large, default, small, mini
            plain: 是否为朴素按钮
            round: 是否为圆角按钮
        """
        super().__init__(text, parent)
        
        # 使用内部变量存储属性值，避免递归
        self._type = button_type
        self._size = size
        self._plain = plain
        self._round = round
        
        # 设置样式表
        self.setStyleSheet(Theme().get_style_sheet("button"))
        
        # 设置属性，用于样式表选择器
        self.setProperty("type", self._type)
        self.setProperty("size", self._size)
        self.setProperty("plain", str(self._plain).lower())
        self.setProperty("round", str(self._round).lower())
        
        # 强制更新样式
        self.style().unpolish(self)
        self.style().polish(self)
        self.update()
    
    def update_style(self):
        """更新按钮样式"""
        # 设置属性，用于样式表选择器
        self.setProperty("type", self._type)
        self.setProperty("size", self._size)
        self.setProperty("plain", str(self._plain).lower())
        self.setProperty("round", str(self._round).lower())
        
        # 强制更新样式
        self.style().unpolish(self)
        self.style().polish(self)
        self.update()
    
    @pyqtProperty(str)
    def button_type(self):
        """获取按钮类型"""
        return self._type
    
    @button_type.setter
    def button_type(self, value):
        """设置按钮类型"""
        if value in ["primary", "success", "warning", "danger", "info"] and value != self._type:
            self._type = value
            self.update_style()
    
    @pyqtProperty(str)
    def size(self):
        """获取按钮尺寸"""
        return self._size
    
    @size.setter
    def size(self, value):
        """设置按钮尺寸"""
        if value in ["large", "default", "small", "mini"] and value != self._size:
            self._size = value
            self.update_style()
    
    @pyqtProperty(bool)
    def plain(self):
        """获取是否为朴素按钮"""
        return self._plain
    
    @plain.setter
    def plain(self, value):
        """设置是否为朴素按钮"""
        if value != self._plain:
            self._plain = value
            self.update_style()
    
    @pyqtProperty(bool)
    def round(self):
        """获取是否为圆角按钮"""
        return self._round
    
    @round.setter
    def round(self, value):
        """设置是否为圆角按钮"""
        if value != self._round:
            self._round = value
            self.update_style()
    
    def sizeHint(self):
        """根据按钮尺寸返回建议大小"""
        base_size = super().sizeHint()
        
        if self._size == "large":
            return QSize(base_size.width() + 20, base_size.height() + 10)
        elif self._size == "small":
            return QSize(base_size.width() - 10, base_size.height() - 5)
        elif self._size == "mini":
            return QSize(base_size.width() - 20, base_size.height() - 10)
        
        return base_size 